/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReturnPanel.java
 *
 * Created on 2011-12-1, 2:47:31
 */
package mscs.cashier.view;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import mscs.cashier.logic.ReturnGoodsUtil;
import mscs.data.pojo.SaleRecord;

/**
 *
 * @author Cake
 */
public class ReturnPanel extends javax.swing.JPanel {

    /** Creates new form ReturnPanel */
    public ReturnPanel() {
        initComponents();
        
    }

     
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mscs.cashier.view.CashierApp.class).getContext().getResourceMap(ReturnPanel.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane1.border.title"))); // NOI18N
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(goodsTableModel);
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane2.border.title"))); // NOI18N
        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable2.setModel(searchTableModel);
        jTable2.setName("jTable2"); // NOI18N
        jScrollPane2.setViewportView(jTable2);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane3.border.title"))); // NOI18N
        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTable3.setModel(returnRecordModel);
        jTable3.setName("jTable3"); // NOI18N
        jScrollPane3.setViewportView(jTable3);

        jScrollPane4.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane4.border.title"))); // NOI18N
        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTable4.setModel(giftTableModel);
        jTable4.setName("jTable4"); // NOI18N
        jScrollPane4.setViewportView(jTable4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
   // TODO add your handling code here:
       this.refreshSearchTable();
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
       if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        if(jTable2.getRowCount()==1){
          
               SaleRecord newSaleRecord=new SaleRecord();
               newSaleRecord.setId(jTextField1.getText());
            
               if(returnGoodsUtil.setSaleTable(newSaleRecord)){
                      this.refreshInfoTable();
                      jTextField1.setText(null);           
                }else {
                   JOptionPane.showMessageDialog(this, "请核实销售记录编号");
               }
               
        }else if(jTable2.getRowCount()==0){
            JOptionPane.showMessageDialog(this, "列表中没有销售记录");
        }else {
            JOptionPane.showMessageDialog(this, "列表中销售记录不只一个");
        }   
      }
       
        if(evt.getKeyCode() == KeyEvent.VK_SHIFT){
            this.showReturnGoodsDialog();
        }
    }//GEN-LAST:event_jTextField1KeyPressed

 
      public boolean isGoodsEnough(int leftNum,int num){
       if(leftNum!=0&&leftNum>=num) {
           return true;
       }
       else if (leftNum==0){
           return false;
       }
       else {
           return false;
       }
   }
   
   public void setEnvironment(){
       
   }
   
   public void showReturnGoodsDialog(){
       ReturnGoodsDialog returnGoodsDialog=new ReturnGoodsDialog(new javax.swing.JFrame(),true);
       returnGoodsDialog.setReturnRecord(returnGoodsUtil.getSaleRecord());
      
       
       returnGoodsDialog.setVisible(true);
   }
  
   
   
    public void refreshInfoTable(){
        this.goodsTableModel.setDataVector(returnGoodsUtil.goodsTableVector(), TableModelFactory.SALEGOODS_COLUMN_NAMES);
        this.giftTableModel.setDataVector(returnGoodsUtil.giftsTableVector(), TableModelFactory.GIFTS_COLUMN_NAMES);
        this.returnRecordModel.setDataVector(returnGoodsUtil.returnRecordTableVector(), TableModelFactory.RETURNRECORD_COLUMN_NAMES);
    }
    
    
    public void refreshSearchTable() {
        this.searchTableModel.setDataVector(searchSaleUtil.searchSaleRecordTableVector( "id",this.jTextField1.getText()), TableModelFactory.SEARCHSALERECORD_COLUMN_NAMES);    
    }
    
   
    
    
    private DefaultTableModel goodsTableModel = MyTableModel.getSaleGoodsModel();
    private DefaultTableModel giftTableModel = TableModelFactory.getGiftsModel();
    private DefaultTableModel returnRecordModel=TableModelFactory.getReturnRecordListModel();
    
    private DefaultTableModel searchTableModel = TableModelFactory.getSearchSaleRecordModel();
     
    private ReturnGoodsUtil returnGoodsUtil = new ReturnGoodsUtil();
    private ReturnGoodsUtil searchSaleUtil = new ReturnGoodsUtil();
    
    
    
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable4;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
